package com.neusoft.neuiotms.dcom.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.PriorityConverter;
import com.neusoft.neuiotms.dcom.dto.PriorityDTO;
import com.neusoft.neuiotms.dcom.model.Priority;
import com.neusoft.neuiotms.dcom.repository.PriorityRepository;
import com.neusoft.neuiotms.dcom.service.PriorityService;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:06:12
 */
@Service
public class PriorityServiceImpl implements PriorityService {
    @Autowired
    private PriorityRepository priorityRepository;

    @Autowired
    private PriorityConverter priorityConverter;

    @Override
    public List<PriorityDTO> index(final Searchable searchable) {
        final List<Priority> models = this.priorityRepository.index(searchable);
        return this.priorityConverter.toListDTO(models);
    }

    @Override
    public Page<PriorityDTO> page(final Searchable searchable, final Pageable pageable) {
        final Page<Priority> models = this.priorityRepository.page(searchable, pageable);
        return this.priorityConverter.toPageDTO(models);
    }

    @Override
    public PriorityDTO create(final PriorityDTO dto) {
        this.validatePriorityExisted(dto, null);

        final Priority model = this.priorityConverter.toModel(dto);
        this.priorityRepository.save(model);
        return this.priorityConverter.toDTO(model);
    }

    private void validatePriorityExisted(final PriorityDTO dto, final Long id) {
        final Searchable Searchable = new Searchable();
        Searchable.put("coverageInfluence", dto.getCoverageInfluence());
        Searchable.put("urgency", dto.getUrgency());
        Searchable.put("priority", dto.getPriority());
        final List<Priority> list = this.priorityRepository.index(Searchable);

        if (id != null) {
            final List<Priority> sub = list.stream().filter(p -> p.getId() != id).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sub) && (sub.size() > 0)) {
                throw new UnitedException(ExceptionConstants.PRIORITY_IS_EXISTED, new Object[] {dto });
            }
        } else {
            if (CollectionUtils.isNotEmpty(list) && (list.size() > 0)) {
                throw new UnitedException(ExceptionConstants.PRIORITY_IS_EXISTED, new Object[] {dto });
            }
        }
    }

    @Override
    public PriorityDTO update(final Long id, final PriorityDTO dto) {
        this.validatePriorityExisted(dto, id);

        Priority model;
        try {
            model = this.priorityRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        model = this.priorityConverter.toModel(dto);
        this.priorityRepository.save(model);
        return this.priorityConverter.toDTO(model);
    }

    @Override
    public PriorityDTO detail(final Long id) {
        Priority model;
        try {
            model = this.priorityRepository.getOne(id);
            return this.priorityConverter.toDTO(model);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
    }

    @Override
    public void delete(final Long id) {
        try {
            this.priorityRepository.deleteById(id);
        } catch (final Exception e) {
            throw new UnitedException("已经处理，暂不允许删除", new Object[] {id });
        }
    }

}
